package com.example.fieldnotes;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

@SuppressWarnings("unused")
public class Home extends Activity {
        private static final int REQUEST_LINK_TO_DBX = 0;
        private Button begin, load;
        private RadioButton radio1, radio2, radio3, radio4;
        private RadioGroup all;
        private ImageView preview;
        private String choice = "", previous = "", file = "";
        private SharedPreferences pref;
        private int setOption = -1;
        private AlertDialog alertDialog;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.home);
                all = (RadioGroup)findViewById(R.id.radioChoice);
                radio1 = (RadioButton)findViewById(R.id.basicNotes);
                radio2 = (RadioButton)findViewById(R.id.scoring);
                radio3 = (RadioButton)findViewById(R.id.anthesis);
                //radio4 = (RadioButton)findViewById(R.id.radioButton4);
                preview = (ImageView)findViewById(R.id.previews);
                pref = getSharedPreferences("Options", 0);
                previous = pref.getString("previous", "");
                
                //Set up buttons
                setUpButtons();
                
                ///////////////////////////////////////
                Bundle boosh = getIntent().getExtras();
                if (boosh != null)
                {
                    file = boosh.getString("loadingFile");
                    ////System.out.println("Received file: " + file);
                    
                    alertDialog = new AlertDialog.Builder(Home.this).create();
                    alertDialog.setTitle("Successfully Loaded File:");
                    alertDialog.setMessage(file);
                    alertDialog.show();
                }
                
                ///////////////////////////////////////////
                
        }

        public void setUpButtons() {
                all.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                                // TODO Auto-generated method stub
                                RadioButton temp = (RadioButton)findViewById(checkedId);
                                begin.setEnabled(true);
                                choice = temp.getText().toString();
                                
                                if (choice.matches("Note Taking"))
                                    preview.setImageResource(R.drawable.notes);
                                else if (choice.matches("Rating"))
                                    preview.setImageResource(R.drawable.rating);
                                else if (choice.matches("Date Keeping"))
                                	preview.setImageResource(R.drawable.dates);
                                else
                                	preview.setImageResource(R.drawable.sample);
                        }
                });
                
                begin = (Button)findViewById(R.id.begin);
                begin.setEnabled(false);
                
                begin.setOnClickListener(new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if (choice.matches("Note Taking"))
                                {
                                	if (!file.matches(""))
                                		file = "notes.xls";
                                    saveOption(setOption=0, file);
                                }
                                else if (choice.matches("Rating"))
                                {
                                	if (!file.matches(""))
                                		file = "rating.xls";
                                    saveOption(setOption=1, file);
                                }
                                else if (choice.matches("Date Keeping"))
                                {
                                	if (!file.matches(""))
                                		file = "dates.xls";
                                    saveOption(setOption=2, file);
                                }
                                else if (choice.matches(""))
                                {
                                	if (!file.matches(""))
                                		file = "notes.xls";
                                    saveOption(setOption=3, file);
                                }
                                else
                                {
                                        //do nothing
                                }
                                
                                if (setOption > -1 && setOption < 4)
                                {
                                        Intent intent = new Intent(Home.this, MainActivity.class);
                                        Home.this.startActivity(intent);
                                }
                        }
        });
            
                
        load = (Button)findViewById(R.id.load);
        load.setOnClickListener(new Button.OnClickListener() {
	        @SuppressWarnings("deprecation")
	        @Override
	
	        public void onClick(View v) {
	        	Intent i = new Intent(Home.this, FileLoad.class);
	        	startActivityForResult(i, 1);
	        }
	    });
    }
        
    public void saveOption(int input, String stringIn)
    {
            SharedPreferences.Editor editor = pref.edit();
            
            editor.putString("stringIn", stringIn);
            ////System.out.println("Home input: " + stringIn);
            editor.putInt("choice", input);
            editor.commit();
    }
}
